package org.ezplatform.office.meeting.setting.boardroom.web.rest;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.ezplatform.cloud.api.component.popuser.PopUserScopeUtilService;
import org.ezplatform.cloud.api.system.user.UserApiService;
import org.ezplatform.cloud.common.controller.BaseController;
import org.ezplatform.core.web.view.OperationResult;
import org.ezplatform.office.meeting.info.service.ApplyRecordDingService;
import org.ezplatform.office.meeting.setting.boardroom.entity.BoardroomSet;
import org.ezplatform.office.meeting.setting.boardroom.service.BoardroomSetService;
import org.ezplatform.office.meeting.setting.category.entity.BoardroomCategorySet;
import org.ezplatform.office.meeting.setting.category.service.BoardroomCategorySetService;
import org.ezplatform.office.meeting.setting.serve.entity.ServeSet;
import org.ezplatform.office.meeting.setting.serve.service.ServeSetService;
import org.ezplatform.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * 
 *会议管理--会议室
 *
 */
@RestController("boardroomsetsfac")
@RequestMapping("api/meeting/boardroomSet")
public class BoardroomSetApiController extends BaseController {

    private final Logger logger = LoggerFactory.getLogger(BoardroomSetApiController.class);
    
    @Autowired
    private BoardroomSetService boardroomSetService;
    
    @Autowired
    private UserApiService userApiService;
    
    @Autowired
    private PopUserScopeUtilService popUserScopeUtilService;
    
    @Autowired
    private ServeSetService serveSetService;
    
    @Autowired
    private BoardroomCategorySetService boardroomCategorySetService;
    
    @Autowired
    private ApplyRecordDingService applyRecordDingService;
    
    /**
     * 会议室
     */
    @ApiOperation(value="会议室", notes="查询会议室list列表数据")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "corpId", value = "查询条件", required = true, paramType = "query", dataType = "String"),
        @ApiImplicitParam(name = "condition", value = "查询条件", required = true, paramType = "query", dataType = "String")
    })
    @RequestMapping(value = "/boardroomSetList", method = RequestMethod.GET,produces={"application/json; charset=UTF-8"})
    public OperationResult getBoardroomSetList(HttpServletRequest request) {
    	Map<String,Object> map = new HashMap<String,Object>();
    	try{
            //查询条件
            String condition = request.getParameter("condition");
            String corpId = request.getParameter("corpId");
            
            //查询会议通知列表数据
            List<Map<String, Object>> list = this.boardroomSetService.getBoardroomList(corpId,condition);
            map.put("data", list);
        }catch(Exception e){
        	e.printStackTrace();
        	return OperationResult.buildFailureResult("获取失败");
        }
        return OperationResult.buildSuccessResult(map);
    }
    
    /**
     * 会议室
     */
    @ApiOperation(value="会议室", notes="查询会议室管理员信息")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "corpId", value = "查询条件", required = true, paramType = "query", dataType = "String"),
    	@ApiImplicitParam(name = "token", value = "token", required = true, paramType = "query", dataType = "String"),
    	@ApiImplicitParam(name = "id", value = "会议室id", required = true, paramType = "query", dataType = "String")
    })
    @RequestMapping(value = "/getBoardroomAdmin", method = RequestMethod.GET,produces={"application/json; charset=UTF-8"})
    public OperationResult getBoardroomAdmin(HttpServletRequest request) {
    	Map<String,Object> map = new HashMap<String,Object>();
    	try{
    		//查询条件
    		String id = request.getParameter("id");
    		String corpId = request.getParameter("corpId");
    		String token = request.getParameter("token");
    		
    		//查询会议通知列表数据
    		BoardroomSet room = this.boardroomSetService.findOne(id);
    		String scopeId = room.getAdminUserScope();
            List<String> idList = this.popUserScopeUtilService.getScopeValue("oa_meeting_apply_record_scope", "oa_meeting_boardroom_set", "admin_user_scope", scopeId);
            String ids = "";
            if(idList!=null && idList.size()>0){
            	for(int i=0; i<idList.size(); i++){
                	ids += idList.get(i) + ",";
                }
                if(ids.length()>0){
                	ids = ids.substring(0, ids.length()-1);
                }
                List<String> uuIds = this.userApiService.getUserIdBySysOrThird(ids, "1", "DD", corpId, token);
                map.put("userId", uuIds);
            }
    	}catch(Exception e){
    		e.printStackTrace();
    		return OperationResult.buildFailureResult("获取失败");
    	}
    	return OperationResult.buildSuccessResult(map);
    }
    
    /**
     * 会议室详细页面数据
     * @param request
     * @return
     */
    @ApiOperation(value="会议室", notes="查看会议室详细信息")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "会议室id", required = true, paramType = "query", dataType = "String"),
        @ApiImplicitParam(name = "date", value = "当前时间", required = true, paramType = "query", dataType = "String"),
        @ApiImplicitParam(name = "token", value = "token", required = true, paramType = "query", dataType = "String"),
        @ApiImplicitParam(name = "corpId", value = "corpId", required = true, paramType = "query", dataType = "String")
    })
    @RequestMapping(value = "/boardRoomInfo", method = RequestMethod.GET,produces={"application/json; charset=UTF-8"})
    public OperationResult getAttendNoticeInfo(HttpServletRequest request) {
        Map<String,Object> map = new HashMap<String,Object>();
        try{
            String id = request.getParameter("id");
            String corpId = request.getParameter("corpId");
            String token = request.getParameter("token");
            String date = request.getParameter("date");
            BoardroomSet boardroomSet = this.boardroomSetService.findOne(id);
			String adminUser = boardroomSet.getAdminUserScope();
			String scopeTypeStr = this.popUserScopeUtilService.getScopeTypeValue("oa_meeting_apply_record_scope", "oa_meeting_boardroom_set", "admin_user_scope", StringUtils.null2String(adminUser));
            //管理员
            map.put("adminUsers", "");
            if(StringUtils.isNotEmpty(scopeTypeStr)) {
                String scopeName = this.userApiService.getScopeName(scopeTypeStr, corpId, token);
                map.put("adminUsers", scopeName);
            }
	        BoardroomCategorySet bcs = this.boardroomCategorySetService.findOne(boardroomSet.getCategoryId());
	        String categoryName = "";
	        if(bcs!=null){
	            categoryName = bcs.getName();
	        }
	        map.put("categoryName", categoryName);
            List<Map> server = this.serveSetService.getServeSetList(request, corpId);
            map.put("boardroomSet", boardroomSet);
            String times = this.applyRecordDingService.getApplyRecordByRoomId(id, date, corpId);
            map.put("times", times);
            map.put("server", server);
        }catch(Exception e){
            e.printStackTrace();
            return OperationResult.buildFailureResult("获取失败");
        }
        return OperationResult.buildSuccessResult(map);
    }

    /**
     * 会议室名称和会议服务名称 ++移动端调用++
     * @param request
     * @return
     */
    @ApiOperation(value="会议室和会议服务", notes="会议室名称和会议服务名称")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "会议室id", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "corpId", value = "corpId", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "type", value = "0-会议室，1-会议服务", required = true, paramType = "query", dataType = "String")
    })
    @RequestMapping(value = "/getBoardRoomOrServes", method = RequestMethod.GET,produces={"application/json; charset=UTF-8"})
    public OperationResult getBoardRoomOrServes(HttpServletRequest request) {
        Map<String,Object> map = new HashMap<String,Object>();
        try{
            String id = request.getParameter("id");
            String type = request.getParameter("type");
            String corpId = request.getParameter("corpId");
            if(StringUtils.equals(type, "0")){
                BoardroomSet boardroomSet = this.boardroomSetService.findOne(id);
                map.put("name", boardroomSet.getName());
            }else if(StringUtils.equals(type, "1")){
                String serveName = "";
                if(StringUtils.isNotEmpty(id)){
                    List<ServeSet> serveList = this.serveSetService.getServeSetListByIds(id);
                    if(serveList!=null && serveList.size()>0){
                        for(ServeSet s : serveList){
                            serveName += s.getName() +  ",";
                        }
                        serveName = serveName.substring(0,(serveName.length()-1));
                    }
                }
                map.put("name", serveName);
            }else{
                BoardroomSet boardroomSet = this.boardroomSetService.findOne(id);
                if(boardroomSet != null){
                    map.put("name", boardroomSet.getName());
                    map.put("position", boardroomSet.getPosition());
                    map.put("longitude", boardroomSet.getLongitude());
                    map.put("latitude", boardroomSet.getLatitude());
                }
                if("3".equals(type)){//流程中获取服务
                    List serves = this.serveSetService.getBpmServeTreeDataList(corpId,id);
                    map.put("serves", serves);
                }else{
                    List serves = this.serveSetService.getBoardRoomServeTreeDataList(corpId,id);
                    map.put("serves", serves);
                }
            }
        }catch(Exception e){
            e.printStackTrace();
            return OperationResult.buildFailureResult("获取失败");
        }
        return OperationResult.buildSuccessResult(map);
    }
    
}